package com.smart.community.commons.websocket;

import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 用户WebSocket会话管理类
 * 
 * @author Wu.Liang
 * @since 2025-01-30
 */
@Data
public class UserWebSocketSession implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 会话ID
     */
    private String sessionId;

    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 用户名
     */
    private String username;

    /**
     * 用户类型
     */
    private String userType;

    /**
     * 物业公司ID
     */
    private Long propertyCompanyId;

    /**
     * 连接时间
     */
    private LocalDateTime connectTime;

    /**
     * 最后活跃时间
     */
    private LocalDateTime lastActiveTime;

    /**
     * 连接状态：CONNECTED-已连接，DISCONNECTED-已断开
     */
    private String status;

    /**
     * 客户端IP地址
     */
    private String clientIp;

    /**
     * 用户代理
     */
    private String userAgent;

    /**
     * 订阅的房间列表
     */
    private Map<String, String> subscribedRooms;

    /**
     * 会话属性
     */
    private Map<String, Object> attributes;

    /**
     * 构造函数
     */
    public UserWebSocketSession() {
        this.sessionId = java.util.UUID.randomUUID().toString();
        this.connectTime = LocalDateTime.now();
        this.lastActiveTime = LocalDateTime.now();
        this.status = "CONNECTED";
        this.subscribedRooms = new ConcurrentHashMap<>();
        this.attributes = new ConcurrentHashMap<>();
    }

    /**
     * 构造函数
     * 
     * @param userId 用户ID
     * @param username 用户名
     * @param userType 用户类型
     * @param propertyCompanyId 物业公司ID
     */
    public UserWebSocketSession(Long userId, String username, String userType, Long propertyCompanyId) {
        this();
        this.userId = userId;
        this.username = username;
        this.userType = userType;
        this.propertyCompanyId = propertyCompanyId;
    }

    /**
     * 更新最后活跃时间
     */
    public void updateLastActiveTime() {
        this.lastActiveTime = LocalDateTime.now();
    }
    
    /**
     * 设置客户端IP地址
     * 
     * @param clientIp 客户端IP地址
     */
    public void setClientIp(String clientIp) {
        this.clientIp = clientIp;
    }
    
    /**
     * 设置用户代理
     * 
     * @param userAgent 用户代理
     */
    public void setUserAgent(String userAgent) {
        this.userAgent = userAgent;
    }

    /**
     * 订阅房间
     * 
     * @param roomId 房间ID
     * @param roomName 房间名称
     */
    public void subscribeRoom(String roomId, String roomName) {
        this.subscribedRooms.put(roomId, roomName);
    }

    /**
     * 取消订阅房间
     * 
     * @param roomId 房间ID
     */
    public void unsubscribeRoom(String roomId) {
        this.subscribedRooms.remove(roomId);
    }

    /**
     * 检查是否订阅了指定房间
     * 
     * @param roomId 房间ID
     * @return 是否订阅
     */
    public boolean isSubscribedToRoom(String roomId) {
        return this.subscribedRooms.containsKey(roomId);
    }

    /**
     * 设置会话属性
     * 
     * @param key 属性键
     * @param value 属性值
     */
    public void setAttribute(String key, Object value) {
        this.attributes.put(key, value);
    }

    /**
     * 获取会话属性
     * 
     * @param key 属性键
     * @return 属性值
     */
    public Object getAttribute(String key) {
        return this.attributes.get(key);
    }

    /**
     * 移除会话属性
     * 
     * @param key 属性键
     */
    public void removeAttribute(String key) {
        this.attributes.remove(key);
    }

    /**
     * 检查会话是否过期（30分钟无活动）
     * 
     * @return 是否过期
     */
    public boolean isExpired() {
        return this.lastActiveTime.plusMinutes(30).isBefore(LocalDateTime.now());
    }

    /**
     * 连接状态枚举
     */
    public static class SessionStatus {
        public static final String CONNECTED = "CONNECTED";         // 已连接
        public static final String DISCONNECTED = "DISCONNECTED";   // 已断开
        public static final String EXPIRED = "EXPIRED";             // 已过期
    }
}
